"""
表格处理脚本

"""

import pymysql
import sys
from TableOne import Tableone
from TableTwo import Tabletwo
from analyze_table_pre import Table
from conf import *

class AnalyzeTable():
    def __init__(self,**params):
        self.html = params['html']
        self.config = params['config']
        self.database = params['database']
        self.params1 = {'rubbish_word':params['rubbish_word'],
                        'replace_word': params['replace_word'],
                        'get_money': params['get_money'],
                        'config': params['config']}


    #获取十字数据
    def get_data_dict(self):
        data_dict_partone,data_dict_parttwo,record_key = Table(**self.params1).main(self.html,self.config['tag_ls'],self.database)
        return data_dict_partone,data_dict_parttwo,record_key

    #处理函数
    def data_handle(self,data_dict_partone,data_dict_parttwo,record_key):

        if data_dict_partone == 'no table':
            return {'yeji':[],'zb':[]}
        part_one_data = Tableone(**self.params1).part_one_table(data_dict_partone, self.config['tag_ls'], record_key)
        part_two_data = Tabletwo(**self.params1).part_two_table(data_dict_parttwo, self.config['tag_ls'])
        end_dict = {'yeji':part_one_data,'zb':part_two_data}
        yeji_need = ['项目名称', '项目类型', '评标成员', '标段']
        if end_dict['yeji'] not in [[],['record']] :
            tt_dict = {}
            for yeji_needi in yeji_need:
                tt_dict[yeji_needi] = end_dict['zb'][yeji_needi] if yeji_needi in end_dict['zb'] else ''
            for yeji_needi in yeji_need:
                if yeji_needi not in end_dict['yeji']:
                    end_dict['yeji'][yeji_needi] = tt_dict[yeji_needi]
            company_ls = ['第一中标候选人', '第二中标候选人', '第三中标候选人']
            for yeji_needi in company_ls:
                for _ in end_dict['yeji'][yeji_needi]:
                    if '项目名称' not in _:
                        _['项目名称'] = tt_dict['项目名称']
            del tt_dict
        return end_dict

    def main(self):
        data_dict_partone,data_dict_parttwo,record_key = self.get_data_dict()
        end = self.data_handle(data_dict_partone,data_dict_parttwo,record_key)
        return end

def read_module ():

    conn = pymysql.connect(
            host='122.112.153.139',
            port=3306,
            user='root',
            passwd='Lubanjianye999!@',
            charset='utf8',
            database='data_collection_server',
            cursorclass=pymysql.cursors.DictCursor)
    cursor = conn.cursor()
    data_pass = []
    tag_pass = []
    data_dict = ''
    args = {'conn':conn,
            'cursor':cursor}
    conf = Conf(**args)
    config = conf.main()
    confunc = ConFunc(config)
    rubbish_word = confunc.rubbish_word
    replace_word = confunc.replace_word
    get_money = confunc.get_money
    params = {
        'html':'C:/Users/Administrator/Desktop/table_easy/table100.html',
        'config':config,
        'rubbish_word':rubbish_word,
        'replace_word': replace_word,
        'get_money': get_money,
        'database':False
    }
    read = AnalyzeTable(**params)
    end = read.main()
    return end

if __name__ == '__main__':
    print(read_module())